<?php

/**
 * This is the model class for table "member".
 *
 * The followings are the available columns in table 'member':
 * @property string $member_id
 * @property string $first_name
 * @property string $last_name
 * @property string $email
 * @property string $username
 * @property string $password
 * @property string $security_question_id
 * @property string $security_answer
 * @property string $security_code
 * @property string $upline_id
 * @property string $upline_key
 * @property string $agree_to_tos
 * @property string $create_date
 * @property string $create_by
 * @property string $update_date
 * @property string $update_by
 * @property string $status
 * @property string $active
 * @property string $member_type
 * @property string $confirmation_code
 * @property string $package_id
 * @property string $tmp_package_id
 * @property string $package_buy_date
 * @property string $forex_user
 * @property string $plain_password
 * @property string $parent_id
 * @property string $hierarchy
 * @property string $test_account
 * @property float $a_point
 * @property float $b_point
 * @property string $a_child_id
 * @property string $b_child_id
 * @property string $tmp
 * 
 * The followings are the available model relations:
 * @property SecutiryQuestion $securityQuestion
 * @property Package $package
 * @property MemberAccount[] $memberAccounts
 * @property MemberPosition[] $memberPositions
 * @property MemberTransactionHistory[] $memberTransactionHistories
 */
class Member extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Member the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'member';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('first_name, email, username, password, member_type', 'required'),
            array('last_name, security_answer, security_code', 'length', 'max'=>256),
            array('member_type', 'length', 'max'=>10),
            array('status,active,forex_user,test_account, tmp, agree_to_tos', 'length', 'max'=>1),
            array('username', 'unique', 'allowEmpty' => 'false'),
//            array('email', 'unique', 'allowEmpty' => 'false'),
            array('email', 'email'),
            array('parent_id, package_id, security_question_id', 'numerical'),
			
            // Please remove those attributes that should not be searched.
            array('member_id, first_name, last_name, email, username, upline_id, status, active, parent_id, test_account, member_type, package_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'securityQuestion' => array(self::HAS_ONE, 'SecurityQuestion', 'secutiry_question_id'),
            'package' => array(self::HAS_ONE, 'Package', 'package_id'),
            'memberAccounts' => array(self::HAS_MANY, 'MemberAccount', 'member_id'),
            'memberPositions' => array(self::HAS_MANY, 'MemberPosition', 'member_id'),
            'memberTransactionHistories' => array(self::HAS_MANY, 'MemberTransactionHistory', 'member_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() { 
        return array(
            'member_id' => 'Member',
            'first_name' => 'First Name',
            'last_name' => 'Last Name',
            'email' => 'Email',
            'username' => 'Username',
            'password' => 'Password',
            'security_question_id' => 'Security Question',
            'security_answer' => 'Security Answer',
            'security_code' => 'Security Code',
            'upline_id' => 'Upline',
            'agree_to_tos' => 'Agree To Tos',
            'create_date' => 'Reg Date',
            'create_by' => 'Create By',
            'update_date' => 'Update Date',
            'update_by' => 'Update By',
            'status' => 'Status',
            'active' => 'Active',
            'forex_user' => 'User only for forex news provider',
            'package_id' => 'Package Id',
            'plain_password' => 'Password',
            'parent_id' => 'Refferer Id',
            'hierarchy' => 'Hierarchy',
            'test_account' => 'Is test account?',
            'a_point' => 'A point',
            'b_point' => 'B point',
            'member_type' => 'Member Type',
            'a_child_id' => 'A Child Id',
            'b_child_id' => 'B Child Id',
            'tmp' => 'Temporary Account',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('member_id', $this->member_id, true); 
        $criteria->compare('first_name', $this->first_name, true);
        $criteria->compare('last_name', $this->last_name, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('username', $this->username, true); 
        $criteria->compare('password', $this->password, true); 
        $criteria->compare('security_question_id', $this->security_question_id, true);
        $criteria->compare('security_answer', $this->security_answer, true);
        $criteria->compare('security_code', $this->security_code, true);
        $criteria->compare('upline_id', $this->upline_id, true);
        $criteria->compare('agree_to_tos', $this->agree_to_tos, true);
        $criteria->compare('create_date', $this->create_date, true);
        $criteria->compare('create_by', $this->create_by, true);
        $criteria->compare('update_date', $this->update_date, true);
        $criteria->compare('update_by', $this->update_by, true);
        $criteria->compare('status', $this->status, true);
        $criteria->compare('forex_user', $this->forex_user, true);
        $criteria->compare('parent_id', $this->parent_id, true);
        $criteria->compare('test_account', $this->test_account, true);
        if(isset($this->active)){
            $criteria->compare('active', $this->active, true);
        } else {
            $criteria->compare('active', 1);
        }
        

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }
    
    public function generateReferralCode(){
        return substr(sha1($this->email), 0, 4);
    }

}